package cn.dkdoo.utils.templateDefault.redis;

import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.*;

import java.util.Collection;
import java.util.List;
import java.util.concurrent.TimeUnit;

/**
 * Created by Nicky.Tang on 2021/5/13 11:34 下午
 *
 * @since 02.12.10
 */
public class DefaultRedisTemplate extends StringRedisTemplate implements ListOperations<String, String> {

    protected String beanName;
    private RollbackRedisTemplate<String, String> templateRollback;

    //private RollbackListOperations<String, String> listOps;

    public DefaultRedisTemplate(String beanName) {
        this.beanName = beanName;
        this.templateRollback = new RollbackRedisTemplate<>(this, beanName);
    }

    public DefaultRedisTemplate(RedisConnectionFactory connectionFactory, String beanName) {
        super(connectionFactory);
        //super.setConnectionFactory(connectionFactory);
        this.beanName = beanName;
        this.templateRollback = new RollbackRedisTemplate<>(this, beanName);
    }

    public DefaultRedisTemplate() {
        char[] cs = DefaultRedisTemplate.class.getSimpleName().toCharArray();
        //首字母大写到小写
        cs[0] += 32;
        this.beanName = String.valueOf(cs);
        this.templateRollback = new RollbackRedisTemplate<>(this, beanName);
    }

//    public RollbackListOperations<String, String> opsForListRol() {
//        if (listOps == null) {
//            listOps = new RollbackListOperations<>(this, this.beanName);
//        }
//        return listOps;
//    }


    @Override
    public List<String> range(String key, long start, long end) {
        List<String> res = this.opsForList().range(key, start, end);
        this.templateRollback.range(key, start, end);
        return res;
    }

    @Override
    public void trim(String key, long start, long end) {
        this.opsForList().trim(key, start, end);
        this.templateRollback.trim(key, start, end);
    }

    @Override
    public Long size(String key) {
        Long res = this.opsForList().size(key);
        this.templateRollback.size(key);
        return res;
    }

    @Override
    public Long leftPush(String key, String value) {
        Long res = this.opsForList().leftPush(key, value);
        this.templateRollback.leftPush(key, value);
        return res;
    }

    @Override
    public Long leftPushAll(String key, String... values) {
        Long res = this.opsForList().leftPushAll(key, values);
        this.templateRollback.leftPushAll(key, values);
        return res;
    }

    @Override
    public Long leftPushAll(String key, Collection<String> values) {
        Long res = this.opsForList().leftPushAll(key, values);
        this.templateRollback.leftPushAll(key, values);
        return res;
    }

    @Override
    public Long leftPushIfPresent(String key, String value) {
        Long res = this.opsForList().leftPushIfPresent(key, value);
        this.templateRollback.leftPushIfPresent(key, value);
        return res;
    }

    @Override
    public Long leftPush(String key, String pivot, String value) {
        Long res = this.opsForList().leftPush(key, pivot, value);
        this.templateRollback.leftPush(key, pivot, value);
        return res;
    }

    @Override
    public Long rightPush(String key, String value) {
        Long res = this.opsForList().rightPush(key, value);
        this.templateRollback.rightPush(key, value);
        return res;
    }

    @Override
    public Long rightPushAll(String key, String... values) {
        Long res = this.opsForList().rightPushAll(key, values);
        this.templateRollback.rightPushAll(key, values);
        return res;
    }

    @Override
    public Long rightPushAll(String key, Collection<String> values) {
        Long res = this.opsForList().rightPushAll(key, values);
        this.templateRollback.rightPushAll(key, values);
        return res;
    }

    @Override
    public Long rightPushIfPresent(String key, String value) {
        Long res = this.opsForList().rightPushIfPresent(key, value);
        this.templateRollback.rightPushIfPresent(key, value);
        return res;
    }

    @Override
    public Long rightPush(String key, String pivot, String value) {
        Long res = this.opsForList().rightPush(key, pivot, value);
        this.templateRollback.rightPush(key, pivot, value);
        return res;
    }

    @Override
    public void set(String key, long index, String value) {
        this.opsForList().set(key, index, value);
        this.templateRollback.set(key, index, value);
    }

    @Override
    public Long remove(String key, long i, Object value) {
        Long res = this.opsForList().remove(key, i, value);
        this.templateRollback.remove(key, i, value);
        return res;
    }

    @Override
    public String index(String key, long index) {
        String res = this.opsForList().index(key, index);
        this.templateRollback.index(key, index);
        return res;
    }

    @Override
    public String leftPop(String key) {
        String res = this.opsForList().leftPop(key);
        this.templateRollback.leftPop(key);
        return res;
    }

    @Override
    public String leftPop(String key, long timeout, TimeUnit unit) {
        String res = this.opsForList().leftPop(key, timeout, unit);
        this.templateRollback.leftPop(key, timeout, unit);
        return res;
    }

    @Override
    public String rightPop(String key) {
        String res = this.opsForList().rightPop(key);
        this.templateRollback.rightPop(key);
        return res;
    }

    @Override
    public String rightPop(String key, long timeout, TimeUnit unit) {
        String res = this.opsForList().rightPop(key, timeout, unit);
        this.templateRollback.rightPop(key, timeout, unit);
        return res;
    }

    @Override
    public String rightPopAndLeftPush(String sourceKey, String destinationKey) {
        String res = this.opsForList().rightPopAndLeftPush(sourceKey, destinationKey);
        this.templateRollback.rightPopAndLeftPush(sourceKey, destinationKey);
        return res;
    }

    @Override
    public String rightPopAndLeftPush(String sourceKey, String destinationKey, long timeout, TimeUnit unit) {
        String res = this.opsForList().rightPopAndLeftPush(sourceKey, destinationKey, timeout, unit);
        this.templateRollback.rightPopAndLeftPush(sourceKey, destinationKey, timeout, unit);
        return res;
    }

    @Override
    public RedisOperations<String , String> getOperations() {
        return this.opsForList().getOperations();
    }

}
